package com.hzb.LNReader;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageViewerActivity extends Activity {
	public static final String DST_IMG = "DST_IMG";
	private Matrix matrix;
	private ImageView imgView;
	private int screenWidth, screenHeight;
	private int rawWidth, rawHeight;
	
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String imgPath = getIntent().getStringExtra(DST_IMG);
//		String imgPath = "/sdcard/ttt.png";
		Bitmap bmp = BitmapFactory.decodeFile(imgPath);
//		Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.untitled);
		if (bmp == null) {
			finish();
		}
		imgView = new ImageView(this);
		imgView.setImageBitmap(bmp);
		imgView.setScaleType(ScaleType.MATRIX);
		imgView.setHorizontalScrollBarEnabled(true);
		imgView.setVerticalScrollBarEnabled(true);
		rawWidth = currWidth = bmp.getWidth();
		rawHeight = currHeight = bmp.getHeight();
		matrix = new Matrix();
		imgView.setImageMatrix(matrix);
		imgView.setOnTouchListener(new OnTouchListener(){
			private GestureDetector detector = new GestureDetector(new ImageViewerGestureListener());
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return detector.onTouchEvent(event);
			}
		});
		setContentView(imgView);
		imgView.post(new Runnable() {
			@Override
			public void run() {
				screenHeight = imgView.getHeight();
				screenWidth = imgView.getWidth();
			}
		});
	}
	
	private int currWidth, currHeight;
	private float[] matrixValue = new float[9];
	private void updateCurrSize() {
		matrix.getValues(matrixValue);
		currWidth = (int) (rawWidth * matrixValue[0]);
		currHeight = (int) (rawHeight * matrixValue[0]);
	}
	public void zoomIn(int x, int y) {
		matrix.postScale(1.25f, 1.25f);
		updateCurrSize();
		int ax = imgView.getScrollX() + x;
		int ay = imgView.getScrollY() + y;
		ax = ax * 125 / 100 - x;
		ay = ay * 125 / 100 - y;
		imgView.setImageMatrix(matrix);
		if (currWidth < screenWidth) ax = 0;
		if (currHeight < screenHeight) ay = 0;
		imgView.scrollTo(ax, ay);
	}
	
	public void zoomOut(int x, int y) {
		matrix.postScale(0.75f, 0.75f);
		updateCurrSize();
		int ax = imgView.getScrollX() + x;
		int ay = imgView.getScrollY() + y;
		ax = ax * 75 / 100 - x;
		ay = ay * 75 / 100 - y;
		imgView.setImageMatrix(matrix);
		if (screenWidth > currWidth || ax < 0) ax = 0;
		else if (ax + screenWidth > currWidth) ax = currWidth - screenWidth;
		if (screenHeight > currHeight || ay < 0) ay = 0;
		else if (ay + screenHeight> currHeight) ay = currHeight - screenHeight;
		imgView.scrollTo(ax, ay);
	}
	
	public void scrollBy(int dx, int dy) {
		int sx = imgView.getScrollX();
		if (screenWidth > currWidth || sx < -dx) dx = -sx;
		else if (sx + dx + screenWidth> currWidth) dx = currWidth - screenWidth - sx;

		int sy = imgView.getScrollY();
		if (screenHeight > currHeight || sy < -dy) dy = -sy;
		else if (sy + dy + screenHeight> currHeight) dy = currHeight - screenHeight - sy;

		imgView.scrollBy(dx, dy);
	}

	class ImageViewerGestureListener extends SimpleOnGestureListener {
		
		public ImageViewerGestureListener() {
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			zoomOut((int)e.getX(), (int)e.getY());
			return true;
		}

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			zoomIn((int)e.getX(), (int)e.getY());
			return true;
		}
		@Override
		public boolean onDown(MotionEvent e) {
			return true;
		}
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			scrollBy((int) distanceX, (int) distanceY);
			return true;
		}
	}

}


